<template>
    <page-wrapper>
        <table-search v-if="isSearch" @search="search(1)" @reset="reset()">
            <template slot="form">
                <form-item label="">
                    <a-input v-model.trim="form.flowName" placeholder="输入检索名称" />
                </form-item>
                <form-item label="">
                    <a-tree-select
                        v-model="form.deptId"
                        allowClear
                        style="width:240px"
                        :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                        :tree-data="deptList"
                        :replaceFields="{children:'children',
                        title:'deptName',
                        key:'deptId',
                        value: 'deptId' }"
                        placeholder="请选择项目部"
                    >
                    </a-tree-select>
                </form-item>
                <form-item label="">
                    <a-select v-model="form.flowType" style="width: 120px" allowClear placeholder="请选择类型">
                        <a-select-option :value="type.value" v-for="type in flowTypes" :key="type.value">{{type.label}}</a-select-option>
                    </a-select>
                </form-item>
                <form-item label="">
                    <a-select v-model="form.status" style="width: 120px" allowClear placeholder="请选择状态">
                        <a-select-option :value="type.value" v-for="type in flowStatus" :key="type.value">{{type.label}}</a-select-option>
                    </a-select>
                </form-item>
            </template>
            <template slot="extra">
                <a-button
                    class="filter-item"
                    type="primary"
                    icon="plus"
                    @click="showInfo()"
                    >新建流程</a-button
                >
            </template>
        </table-search>
        <div>
            <table-list
                :loading="loading"
                :columns="columns"
                :data="list"
                rowKey="id"
                :pagination="pagination"
            >
                <template slot="index" slot-scope="{ index }">
                    <span>{{ (form.page - 1) * form.size + index + 1 }}</span>
                </template>
                <template slot="deptId" slot-scope="{ text }">
                    <field-tag :value="text" type="deptId" showType="deptName" :data="expandDeptData"></field-tag>
                </template>
                <template slot="flowType" slot-scope="{ text }">
                    <field-tag :value="text.flowType" type="value" showType="label" :data="flowTypes"></field-tag>
                </template>
                <template slot="status" slot-scope="{ text }">
                    <a-switch
                        checked-children="启用"
                        un-checked-children="未启用"
                        :key="text.id"
                        :default-checked="text.status == '01' ? true : false"
                        @change="onSwitchStatus(text)"
                    />
                </template>
                <template slot="action" slot-scope="{ text, index }">
                    <a-button
                        size="small"
                        type="primary"
                        @click="showInfo(text, index)"
                        >编辑</a-button
                    >
                    <a-button
                        size="small"
                        class="btn_space"
                        type="danger"
                        @click="remove(text)"
                        :disabled="text.status=='01'?true:false"
                    >删除</a-button>
                </template>
            </table-list>
        </div>

        <template v-if="isShowInfo">
            <process-info :info="itemInfo" :deptList="deptList" @cancel="isShowInfo = false" @submit="submit" />
        </template>
    </page-wrapper>
</template>
<script>
import table from '@/mixins/table';
import { columns } from './columns';
import ProcessInfo from './components/info.vue';
import deptApi from '@/api/system/dept';
import { remoteList, deleteInfo,updateInfo } from '@/api/system/worker';
import {flowTypes,flowStatus} from './dictionary'

export default {
    name: 'ProcessList',
    mixins: [table],
    props: {
        params: {
            //列表接口筛选的参数
            type: Object,
            default: () => {}
        },
        isSearch: {
            type: Boolean,
            default: true
        }
    },
    components: {
        ProcessInfo
    },
    data() {
        let originForm = {
            flowName: null,
            status: undefined,
            flowType: undefined,
            deptId: undefined
        };
        return {
            originForm,
            columns,
            flowTypes,flowStatus,
            form: {
                ...originForm
            },
            itemInfo: null,
            isShowInfo: false,
            list: [],
            deptList: [],
            roleList:[],
            expandDeptData: [],
            isShowPwd: false,
            isPreview: false,
            previewUrl: ''
        };
    },
    watch: {
        params: {
            handler(now, pre) {
                if (JSON.stringify(now) == JSON.stringify(pre)) return false;
                this.form = { ...this.form, ...now };
                this.search(1);
            },
            deep: true
        }
    },
    async created() {
        await this.getResource();
        this.search(1);
    },
    methods: {
        async search(page) {
            try {
                this.loading = true;
                if (page) {
                    this.form.page = page;
                }
                this.list = [];
                let res = await remoteList(this.form);
                let { list, total } = res.data;
                this.list = list;
                this.total = total;
            } finally {
                this.loading = false;
            }
        },
        //显示添加用户
        showInfo(item = {}) {
            this.isShowInfo = true;
            this.itemInfo = item;
        },
        //是否开启
        async onSwitchStatus(item) {
            console.log(item);
            try {
                let { status } = item;
                item.status = status == '01' ? '02' : '01';
                this.loading = true;
                await updateInfo(item);
                this.$message.success('操作成功!');
            } catch (err) {
                let { status } = item;
                item.status = status == '01' ? '02' : '01';
            } finally {
                this.loading = false;
            }
        },
        //更新数据
        submit() {
            this.isShowInfo = false;
            this.isShowPwd = false;
            this.search(1);
        },
        showPreview(text) {
            this.isPreview = true;
            this.previewUrl = text;
        },
        //删除
        remove(item = '') {
            this.$confirm({
                title: '提示',
                content: '确定要删除流程信息？',
                okText: '确定',
                cancelText: '取消',
                onOk: async () => {
                    try {
                        let ids = [];
                        if (item) {
                            ids = [item.id];
                        } else {
                            let selectionBox = this.selectionBox;
                            console.log(selectionBox);
                            selectionBox.map(async item => {
                                ids.push(item.id);
                            });
                        }
                        await deleteInfo({ ids });
                        this.$message.success('删除成功!');
                        this.search(1);
                    } catch (err) {}
                },
                onCancel() {}
            });
        },
        //获取资源列表信息
        async getResource() {
            let deptResult = await deptApi.remoteList();
            this.deptList = deptResult.data;
            this.expandDeptData = [];
            this.expandData(deptResult.data);
        },
        //多层次数据展开
        expandData(data) {
            if (Array.isArray(data)) {
                data.map(item => {
                    this.expandDeptData.push(item);
                    if (item.children && item.children.length) {
                        this.expandData(item.children);
                    }
                });
            }
        }
    }
};
</script>
